package org.adream.account.rest.api;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.entity.UserAddressEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.UserAddressService;
import org.adream.account.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 	收货地址
 * @author Administrator
 *
 */
@Controller
@RequestMapping("userAddress")
public class UserAddressController {
	
	@Autowired
	UserAddressService userAddressService;
	
	
	/**
	 * 	增加单个地址
	 * @param addressEntity
	 * @param request
	 * @return
	 */
	@RequestMapping(value="add",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> add(UserAddressEntity addressEntity,HttpServletRequest request) {
		if(addressEntity == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "参数错误！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getUserName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人不能为空！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getTelNumber())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人手机不能为空！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getUid())) {
			String uid = UserUtil.getUidByRequest(request);
			addressEntity.setUid(uid);
		}
		userAddressService.add(addressEntity);
		
		return new ResultModel<String>(ResultModel.SUCCESS, "添加成功", null);
		 
	}
	/**
	 * 	根据ID单个删除
	 * @param uaId
	 * @return
	 */
	@RequestMapping(value="delete/{uaId}",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> deleteById(@PathVariable("uaId")Integer uaId) {
		userAddressService.deleteById(uaId);
		return new ResultModel<String>(ResultModel.SUCCESS, "删除成功", null);
	}
	/**
	 *	 修改
	 * @param addressEntity
	 * @param request
	 * @return
	 */
	@RequestMapping(value="update",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> update(UserAddressEntity addressEntity,HttpServletRequest request) {
		if(addressEntity == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "参数错误！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getUserName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人不能为空！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getTelNumber())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人手机不能为空！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getUid())) {
			String uid = UserUtil.getUidByRequest(request);
			addressEntity.setUid(uid);
		}
		userAddressService.update(addressEntity);
		return new ResultModel<String>(ResultModel.SUCCESS, "更新成功", null);
	}
	/**
	 * 	查询全部地址 ，默认地址在首位 
	 * @param request
	 * @return
	 */
	@RequestMapping(value="search",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> search(HttpServletRequest request) {
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			return new ResultModel<String>(ResultModel.ERR_USER_INFO, "用户信息丢失，请重新登录", null);
		}
		List<UserAddressEntity> list = userAddressService.searchByUid(uid);
		return new ResultModel<List<UserAddressEntity>>(ResultModel.SUCCESS, "查询成功", list);
	}
	
	/**
	 * 	根据对象中uaId是否存在  新增 或者 更新 
	 * @param addressEntity
	 * @param request
	 * @return
	 */
	@RequestMapping(value="save",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> save(@RequestBody UserAddressEntity addressEntity,HttpServletRequest request) {
		if(addressEntity == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "参数错误！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getUserName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人不能为空！", null);
		}
		if(StringUtils.isEmpty(addressEntity.getTelNumber())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "收件人手机不能为空！", null);
		}
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(addressEntity.getUid())) {
			addressEntity.setUid(uid);
		}
		Integer uaId = addressEntity.getUaId();
		
		//各 字段的判断 视情况而定 目前不需要做判断
		if(uaId == null) {
			userAddressService.add(addressEntity);
		}else {
			userAddressService.update(addressEntity);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "更新成功", null);
	}
	/**
	 * 	根据ID查询地址
	 * @param uaId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="search/{uaId}",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> searchByUaId(@PathVariable("uaId")Integer uaId,HttpServletRequest request) {
		 if(uaId == null) {
			 return new ResultModel<String>(ResultModel.ERR_PARAM, "参数丢失", null);
		 }
		 UserAddressEntity  userAddressEntity = userAddressService.searchByUaId(uaId);
		 return new ResultModel<UserAddressEntity>(ResultModel.SUCCESS, "查询成功", userAddressEntity);
	}
}
